<?php

/*
 * This class is used for as Model for Subject table 
 */

class Model_Subject extends Model_Base {

    protected $_name = 'tbl_subject';
    protected $_primaryKey = 'subj_id';

    /**
     * Function to return different units of the Subject from the uom table
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    
    
    //Get type data from tbl_list table

    public function gettypeArray($lang_id) {
        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchall('Subject Type');
        $finalArray[''] = '-- Select Type --';
        $parent_id = $ParentdropdownArray[0]['parent_id'];
        $ChilddropdownModel = new Model_List();
        $where = 'child.lang_id = '.$lang_id.' and child.parent_id = '.$parent_id.' ';
        
        $ChilddropdownArray = $ChilddropdownModel->fetchType($where);

        foreach ($ChilddropdownArray as $com) {
            $finalArray[$com['ref_child_id']] = $com['child_name'];
        }
        if (!empty($finalArray)) {
            return $finalArray;
        } else {
            return array();
        }
    }
    
    
    //get department data
    public function getdepartmentArray($where ="" ) {
         
        $departmentModel = new Model_Department();
        $departmentArray = $departmentModel->fetchDepartment($where);
        $finalArray[''] = '';
        if($where != ''){
            foreach ($departmentArray as $com) {
                $finalArray[$com['dep_id']] = $com['dep_name']." [".$com['site_name']."]";
            }
        }
        
        return $finalArray;
    }

    //get site data
     public function getsiteArray($where = "") {
        $siteModel = new Model_Site();
        $siteArray = $siteModel->fetchSite($where);
        $finalArray[''] = '';
        foreach ($siteArray as $com) {
            $finalArray[$com['site_id']] = $com['site_name'];
        }
        return $finalArray;
    }
    /**
     * Function to return different status of the Subject
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
           # '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry for view
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id,$lang_id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('subject' => 'tbl_subject'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = subject.updated_lgn_id")
                ->join(array('dep' => 'tbl_department'), "dep.dep_id = subject.dep_id")
                ->joinLeft(array('type' => 'tbl_list'), "type.child_id = subject.type_id and type.lang_id = ".$lang_id."")
                ->join(array('site' => 'tbl_site'), "site.site_id = subject.site_id")
                ->where('subj_id = ?', $id)
                ->where('subj_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('subject' => 'tbl_subject'))
                ->setIntegrityCheck(false)
                ->where('subj_id = ?', $id)
                ->where('subj_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get subjects
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchSubject($where = "", $limit = "", $offset = "0", $order = 'subj_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('subject' => 'tbl_subject'))
                ->setIntegrityCheck(false)
                ->join(array('dep' => 'tbl_department'), "dep.dep_id = subject.dep_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = subject.site_id")
                ->where('subj_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getSubjectCount($where) {
        $select = $this->select()
                ->from(array('subject' => 'tbl_subject'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('dep' => 'tbl_department'), "dep.dep_id = subject.dep_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = subject.site_id")
                ->where('subj_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete subjects
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteSubject($prdtArray) {

        $config = Zend_Registry::get('config');
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_subject) {
                $subjectData = Array();
                $subjectData = $this->fetchEntryById($_subject);
                if (count($subjectData) > 0) {
                    $data = array('subj_status' => '2');
                    $this->update($data, 'subj_id =' . (int) $_subject);
                    //delete of the images
                    $folder = $config->UPLOAD_PATH . "subject/";
                    $subject_image = $folder . $subjectData['subj_instruction'];
                    if (file_exists($subject_image)) {
                        @unlink($subject_image);
                    }
                }
            }
        } else {
            $subj_id = $prdtArray;
            $subjectData = $this->fetchEntryById($subj_id);
            if (count($subjectData) > 0) {
                $data = array('subj_status' => '2');
                $this->update($data, 'subj_id =' . $subj_id);
                //delete of the images
                $folder = $config->UPLOAD_PATH . "subject/";
                $subject_image = $folder . $subjectData['subj_instruction'];
                if (file_exists($subject_image)) {
                    @unlink($subject_image);
                }
            }
        }
    }

}

?>